package com.k12.app.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.k12.app.interceptors.TenantInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class  K12WebMvcConfigure implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new TenantInterceptor())
                .addPathPatterns("/**").excludePathPatterns("/app/user/login");
        // 注册登录拦截器
        registry.addInterceptor(new SaInterceptor(handler -> {
            // 全局登录校验
            SaRouter.match("/**")
                    // 排除路径
                    .notMatch(
                            "/auth/login",
                            "/auth/logout",
                            "/doc.html",
                            "/webjars/**",
                            "/swagger-resources/**",
                            "/v2/api-docs",
                            "/v3/api-docs/**",
                            "/error",
                            "/favicon.ico",
                            "/public/**",
                            "/app/user/login"
                    )
                    // 检查登录
                    .check(r -> StpUtil.checkLogin());

            // 特定路径的权限校验
            SaRouter.match("/admin/**", () -> StpUtil.checkRole("admin"));
        })).addPathPatterns("/**").order(Ordered.HIGHEST_PRECEDENCE);
    }
}